<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class msoft extends CI_Model {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
    public $mytable = "soft";
    public $primaryKey = "SOFT_ID";
    public $resCode = "SOFT";

    function __construct() {
        parent::__construct();
    }

    function field() {
		$fieldData = array();
		$fieldData['MAN_ID'] = 0;
		$fieldData['LICE_ID'] = 0;
		$fieldData['CAT_ID'] = 0;
		$fieldData['USE_ID'] = 0;
		$fieldData['SOFT_NAME'] = '';
		$fieldData['SOFT_NAME_DESCRIPTION'] = '';
		$fieldData['SOFT_CONTENT'] = '';
		$fieldData['SOFT_FILESIZE'] = '';
		$fieldData['SOFT_CONTENT_SHORT'] = '';
		$fieldData['SOFT_TIME_CREATE'] = time();
		$fieldData['SOFT_TIME_UPDATE'] = time();
		$fieldData['SOFT_STATUS'] = 1;
		$fieldData['SOFT_VIEW'] = 0;
		$fieldData['SOFT_THUMB'] = '';
		$fieldData['SOFT_CODE'] = '';
		$fieldData['SOFT_DOWN'] = 0;
		$fieldData['SOFT_TIME_USE_LIMIT'] = 0;
		$fieldData['SOFT_REQUIREMENT_OS'] = '';
		$fieldData['SEO_H1'] = '';
		$fieldData['SEO_KEYWORD'] = '';
		$fieldData['SEO_DESCRIPTION'] = '';
		$fieldData['SEO_METATITLE'] = '';
		$fieldData['SOFT_VERSION'] = '';
		$fieldData['SOFT_ICON'] = '';
		$fieldData['SOFT_CACHE_MAN'] = null;
		$fieldData['SOFT_CACHE_CATEGORY'] = null;
		$fieldData['SOFT_CACHE_LICENSE'] = null;
		$fieldData['SOFT_CACHE_IMAGES'] = null;
		return $fieldData;
    }

    function get($val = '', $param = null) {
        if ($param == null)
            $param = $this->primaryKey;

        if (is_array($val) && $val != array())
            $this->db->where($val);
        else if ($val != '')
            $this->db->where($param, $val);

        $this->db->from($this->mytable);
        return $this->db->get()->first_row();
    }

    function gets($val = '', $param = null) {
        if ($param == null)
            $param = $this->primaryKey;
        if (is_array($val) && $val != array())
            $this->db->where($val);
        else if ($val != '')
            $this->db->where($param, $val);
		
        $this->db->from($this->mytable);
        return $this->db->get()->result();
    }

    function where_in($val = '', $param = null) {
        if ($param == null)
            $param = $this->primaryKey;

        $this->db->from($this->mytable);
        if ($val != '')
            $this->db->where_in($this->mytable . "." . $param, $val);
        return $this->db->get()->result();
    }

    function num($val = '', $param = null) {
       if ($param == null)
            $param = $this->primaryKey;
        if (is_array($val) && $val != array())
            $this->db->where($val);
        else if ($val != '')
            $this->db->where($param, $val);
		
        $this->db->from($this->mytable);
        return $this->db->count_all_results();
    }

    function update($val = '', $param = null, $arr_update) {
        if ($param == null)
            $param = $this->primaryKey;

        if ($val != '')
            $this->db->where($param, $val);
		$arr_update['SOFT_TIME_UPDATE'] = time();
        $arr_update = $this->msystem->update_security($arr_update, $this->field());
        $this->db->update($this->mytable, $arr_update);
    }

    function insert($arr_insert) {
        $arr_insert = $this->msystem->insert_security($arr_insert, $this->field());
        $this->db->insert($this->mytable, $arr_insert);
        return $this->db->insert_id();
    }

    function delete($val = '', $param = null) {
        if ($val != '') {
            $this->db->where($param, $val);
            $this->db->delete($this->mytable);
        }
    }
	function getsSoft($val = '', $param = null, $type='gets') {
		if ($param == null)
            $param = $this->primaryKey;
        if (is_array($val) && $val != array())
            $this->db->where($val);
        else if ($val != '')
            $this->db->where($param, $val);

        $this->db->from($this->mytable." as p");
		//$this->db->join("category as s", "p.CAT_ID = s.CAT_ID");
		//$this->db->join("license as s1", "p.LICE_ID = s1.LICE_ID");
		//$this->db->join("manufacturer as s2", "p.MAN_ID = s2.MAN_ID");
		
		//return count all result
		if($type =='num')
			return $this->db->count_all_results();
		
		$softs = $this->db->get()->result_array();
		return $this->merge_softs($softs);
	
	}
	
	function getSoft($val = '', $param = null) {
        if ($param == null)
            $param = $this->primaryKey;
        if (is_array($val) && $val != array())
            $this->db->where($val);
        else if ($val != '')
            $this->db->where($param, $val);
		$this->db->select(" s.*,s1.*,s2.*,p.*");
        $this->db->from($this->mytable." as p");
		$this->db->join("category as s", "p.CAT_ID = s.CAT_ID");
		$this->db->join("license as s1", "p.LICE_ID = s1.LICE_ID");
		$this->db->join("manufacturer as s2", "p.MAN_ID = s2.MAN_ID");
        return $this->db->get()->first_row();
    }

	function getsNewUpdate($cateID, $limit = 10){
		$this->db->limit($limit);
		$this->db->order_by('SOFT_TIME_UPDATE desc');
		$this->db->where("p.SOFT_STATUS",1);
		if(is_array($cateID)){
			 $this->db->where_in('p.CAT_ID', $cateID);
		}
		else if($cateID >0) {
		$this->db->where("p.CAT_ID",$cateID);
		}
        $this->db->from($this->mytable." as p");
		$softs = $this->db->get()->result_array();
		return $this->merge_softs($softs);
	}
	
	function getsNewInsert($cateID, $limit = 10){
		$this->db->limit($limit);
		$this->db->order_by('SOFT_TIME_CREATE desc');
		$this->db->where("p.SOFT_STATUS",1);
		if(is_array($cateID)){
			 $this->db->where_in('p.CAT_ID', $cateID);
		}
		else if($cateID >0) {
		$this->db->where("p.CAT_ID",$cateID);
		}
        $this->db->from($this->mytable." as p");
		$softs = $this->db->get()->result_array();
        return $this->merge_softs($softs);
	}
	
	/*
		Lấy danh sách các chương trình mới nhất theo Man ID
	*/
	function getsNewMan($manID = 0, $orderBy = ''){
		$contidion = array();
		$contidion['SOFT_STATUS'] = 1;
		if(is_array($manID)){
			 $this->db->where_in('p.MAN_ID', $manID);
		}
		else
		{
		if($manID >0)
			$contidion['p.MAN_ID'] = $manID;
		}
		
		if($orderBy =='popular'){
			$this->db->order_by('SOFT_DOWN desc');
		}
		else {
			$this->db->order_by('SOFT_TIME_CREATE desc');
		}
		return $this->getsSoft($contidion);
	}
	
	function getsSearch($keyword = '',$manCode = '', $orderBy = '', $type='gets'){
		$contidion = array();
		$contidion['SOFT_STATUS'] = 1;
		if($manCode !=''){
			$contidion['s2.MAN_CODE'] = $manCode;
		}
		$this->db->where("(`SOFT_NAME` LIKE '%".$keyword."%' 
OR `SOFT_NAME_DESCRIPTION` LIKE '%".$keyword."%')"); 
		if($orderBy =='popular'){
			$this->db->order_by('SOFT_DOWN desc');
		}
		else {
			$this->db->order_by('SOFT_TIME_CREATE desc');
		}
		
		return $this->getsSoft($contidion,null,$type);
	}
	
	function getRelated($cateID = 0, $softID = 0, $limit = 10){
		
		$this->db->limit($limit);
		$this->db->limit($limit);
		return $this->gets(array(
			'SOFT_ID <'=>$softID,
			'CAT_ID'=>$cateID,
		));
	}
	
	function updateView($softID){
		//update view
        $this->db->where("SOFT_ID", $softID);
        $this->db->set('SOFT_VIEW', 'SOFT_VIEW+1', false);
        $this->db->update($this->mytable, array());
	}
	
	function updateDownload($softID){
		//update view
        $this->db->where("SOFT_ID", $softID);
        $this->db->set('SOFT_DOWN', 'SOFT_DOWN+1', false);
        $this->db->update($this->mytable, array());
	}
	
	function merge_softs($softs){
		for($i=0; $i<count($softs); $i++){
			$cache = json_decode($softs[$i]['SOFT_CACHE_MAN'],true);
			$softs[$i] = array_merge($cache, $softs[$i]);
			$cache = json_decode($softs[$i]['SOFT_CACHE_CATEGORY'],true);
			$softs[$i] = array_merge($cache, $softs[$i]);
			$cache = json_decode($softs[$i]['SOFT_CACHE_LICENSE'],true);
			$softs[$i] = (object)array_merge($cache, $softs[$i]);
		}
		return (object)$softs;
	}
	
	function getImageDetail($softID){
		$this->db->where("SOFT_ID",$softID);
		$this->db->from("soft_images");
		return $this->db->get()->result();
	
	}
}

?>